package com.bw.module_message.view

import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmFragment
import com.bw.lib_http.Const
import com.bw.module_message.R
import com.bw.module_message.adapter.MessageAdapter
import com.bw.module_message.databinding.FragmentMessageBinding
import com.bw.module_message.viewModel.MsgViewModel
import com.tencent.mmkv.MMKV


@Route(path = RouterPath.MODULE_MESSAGE_FRAGMENT)
class MessageFragment : BaseMvvmFragment<FragmentMessageBinding>() {

    private val messageAdapter = MessageAdapter()
    private val vm by lazy { ViewModelProvider(this)[MsgViewModel::class.java] }

    override fun loadData() {
        super.loadData()
            vm.mSuccessLiveData.observe(this){
                messageAdapter.data.clear()
            messageAdapter.data.addAll(it)
            messageAdapter.notifyDataSetChanged()
        }
    }

    override fun initViews() {
        vm.getMsgList(Const.ACCESS_ID, MMKV.defaultMMKV().decodeInt("userId",0))
        binding.messageFragmentRv.adapter = messageAdapter
        binding.messageFragmentRv.layoutManager = LinearLayoutManager(requireContext())
    }


    override fun observeDataChange() {

    }

    override fun getLayoutId(): Int = R.layout.fragment_message
}